﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
namespace GoomeiPicasaViewer
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
        private bool mouseState = false;
        private Point mousePoint = new Point();
        private string[] fileList;
        private int fileNum;
        private Point orgintSize = new Point();
        private int angle = 0;
		public MainWindow()
		{
			this.InitializeComponent();
            this.Height = SystemParameters.WorkArea.Height;
            this.Width = SystemParameters.WorkArea.Width;
            LayoutRoot.Height = SystemParameters.WorkArea.Height;
            LayoutRoot.Width = SystemParameters.WorkArea.Width;
            string command = Environment.CommandLine;//获取进程命令行参数
            string[] para = command.Split('\"');
            string pathC="";
            if (para.Length > 3)
            {
                pathC = para[3];//获取打开的文件的路径
            }
            else
                pathC = "C:\\Users\\ZHF\\Desktop\\GoomeiPicasaViewer\\GoomeiPicasaViewer\\GoomeiPicasaViewer\\bin\\Debug\\10_full.jpg";
                 BitmapImage image= new BitmapImage(new Uri(pathC, UriKind.RelativeOrAbsolute));
                 Picture.Source = image;
                 orgintSize.X = image.Height;
                 orgintSize.Y = image.Width;
                 Thickness former = PictureViewBox.Margin;
                 former.Left = (LayoutRoot.Width - image.Width) / 2;
                 former.Top = (LayoutRoot.Height - image.Height) / 2;
                 PictureViewBox.Margin = former;
                 PictureViewBox.Width = image.Width;
                 PictureViewBox.Height = image.Height;
                 fileList = System.IO.Directory.GetFiles(pathC.Substring(0,pathC.LastIndexOf('\\')),"*.jpg");       
 
            

			// Insert code required on object creation below this point.
		}
        private void ViewMouseDown(object sender, MouseButtonEventArgs e)
        {
            mouseState = true;
            mousePoint = e.GetPosition(LayoutRoot);
        }

        private void ViewMouseUp(object sender, MouseButtonEventArgs e)
        {
            mouseState = false;
        }

        private void ViewMouseMove(object sender, MouseEventArgs e)
        {
            if (mouseState == true)
            {
                double top = mousePoint.Y - e.GetPosition(LayoutRoot).Y;
                double left = mousePoint.X - e.GetPosition(LayoutRoot).X;
                Thickness former = PictureViewBox.Margin;
                former.Left -= left;
                former.Top -= top;
                PictureViewBox.Margin = former;
                mousePoint = e.GetPosition(LayoutRoot);
            }
        }

        private void ellipse_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        	// TODO: Add event handler implementation here.
			this.Close();
        }
        private void Large()
        {
            Thickness former = PictureViewBox.Margin;
            former.Left -= PictureViewBox.Width * 0.05;
            former.Top -= PictureViewBox.Height * 0.05;
            PictureViewBox.Margin = former;
            PictureViewBox.Height *= 1.1;
            PictureViewBox.Width *= 1.1;
        }
        private void Small()
        {
            Thickness former = PictureViewBox.Margin;
            PictureViewBox.Height /= 1.1;
            PictureViewBox.Width /= 1.1;
            former.Left += PictureViewBox.Width * 0.05;
            former.Top += PictureViewBox.Height * 0.05;
            PictureViewBox.Margin = former;
        }
        private void Origin()
        {
            Thickness former = PictureViewBox.Margin;
            PictureViewBox.Height = orgintSize.X ;
            PictureViewBox.Width =orgintSize.Y ;
            former.Left = (LayoutRoot.Width - orgintSize.Y) / 2;
            former.Top = (LayoutRoot.Height - orgintSize.X) / 2;
            PictureViewBox.Margin = former;
        }
        private void LayoutRoot_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (e.Delta > 0)
            {
                Large();                
            }
            if (e.Delta < 0)
            {
                Small();
            }
        }

        private void Next_Click(object sender, RoutedEventArgs e)
        {
            BitmapImage image = new BitmapImage(new Uri(fileList[fileNum], UriKind.RelativeOrAbsolute));
            ++fileNum;
            if (fileNum == fileList.Length)
                fileNum = 0;
            Picture.Source = image;
            Thickness former = PictureViewBox.Margin;
            former.Left = (LayoutRoot.Width - image.Width) / 2;
            former.Top = (LayoutRoot.Height - image.Height) / 2;
            PictureViewBox.Margin = former;
            PictureViewBox.Width = image.Width;
            PictureViewBox.Height = image.Height;
        }

        private void former_Click(object sender, RoutedEventArgs e)
        {
            BitmapImage image = new BitmapImage(new Uri(fileList[fileNum], UriKind.RelativeOrAbsolute));
            --fileNum;
            if (fileNum == -1)
                fileNum = fileList.Length-1;
            Picture.Source = image;
            Thickness former = PictureViewBox.Margin;
            former.Left = (LayoutRoot.Width - image.Width) / 2;
            former.Top = (LayoutRoot.Height - image.Height) / 2;
            PictureViewBox.Margin = former;
            PictureViewBox.Width = image.Width;
            PictureViewBox.Height = image.Height;
        }
        private void Rotate()
        {
            RotateTransform rotateTransform = new RotateTransform(90*angle);
            rotateTransform.CenterX = Picture.Width / 2;
            rotateTransform.CenterY = Picture.Height / 2;
            Picture.RenderTransform = rotateTransform;
            //PictureViewBox.Width = image.Width;
            //PictureViewBox.Height = image.Height;
        }
        private void CounterClock_Click(object sender, RoutedEventArgs e)
        {
            --angle;
            Rotate();
        }

        private void Large_Grid_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        	// TODO: Add event handler implementation here.
			Large();
        }

        private void Small_Grid_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        	// TODO: Add event handler implementation here.
			Small();
        }

        private void Origin_Grid_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        	// TODO: Add event handler implementation here.
			Origin();
        }

        private void CounterClock_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        	// TODO: Add event handler implementation here.
			 --angle;
            Rotate();
        }

        private void Counter_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        	// TODO: Add event handler implementation here.
			++angle;
			Rotate();
        }

        private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ListViewItem item = (ListViewItem)listImages.SelectedItem;
            BitmapImage image = new BitmapImage(new Uri(item.Tag.ToString(), UriKind.RelativeOrAbsolute));
            Picture.Source = image;
        }
	}
}